package com.lcache.common.redis;

import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.List;

/**
 * @program: LIM
 * @description: redis处理类
 * @author: Alex Wu
 * @createDate: 2025-04-15 10:04
 **/
public class RedisService {
    public void set(String key, String value, long expireSeconds) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            commands.setex(key, expireSeconds, value);
        } catch (Exception e) {
            throw new RuntimeException("Redis set failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public String get(String key) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            return commands.get(key);
        } catch (Exception e) {
            throw new RuntimeException("Redis get failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public void delete(String key) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            commands.del(key);
        } catch (Exception e) {
            throw new RuntimeException("Redis delete failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public void lpush(String key, String messageJson) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            commands.lpush(key, messageJson);
        } catch (Exception e) {
            throw new RuntimeException("Redis lpush failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public List<String> lrange(String key, int start, int end) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            return commands.lrange(key, start, end);
        } catch (Exception e) {
            throw new RuntimeException("Redis lrange failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public void expire(String key, long seconds) {
        StatefulRedisConnection<String, String> connection = null;
        try {
            connection = RedisClientManager.getConnection();
            RedisCommands<String, String> commands = connection.sync();
            commands.expire(key, seconds);
        } catch (Exception e) {
            throw new RuntimeException("Redis expire failed", e);
        } finally {
            if (connection != null) {
                RedisClientManager.returnConnection(connection);
            }
        }
    }

    public void del(String key) {
        delete(key);
    }
}
